/**
 * Copyright 2009 Joe LaPenna
 */

package com.googlecode.dumpcatcher.logging;

import junit.framework.TestCase;

/**
 * @author Joe LaPenna (joe@joelapenna.com)
 */
public class DumpcatcherUncaughtExceptionHandlerTestCase extends TestCase {

    private static final String PRODUCT_KEY = "agtkdW1wY2F0Y2hlcnINCxIHUHJvZHVjdBgCDA";
    private static final String SECRET = "4ceafc4082e345ec98972c00f035609b";
    private static final String CLIENT = "unittest";

    private Dumpcatcher mDumpcatcher;

    public void setUp() {
        mDumpcatcher = new Dumpcatcher(PRODUCT_KEY, SECRET, "http://localhost:8080/add", CLIENT, 2);
    }

    public void test_uncaughtException() throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                throw new MyRuntimeException("BOOM!");
            }
        });

        thread.setUncaughtExceptionHandler(new DumpcatcherUncaughtExceptionHandler(mDumpcatcher));
        thread.start();
        thread.join();
        // TODO(jlapenna): Actually somehow check that the message ended up getting logged.
    }

    public void test_uncaughtExceptionServerUnavailable() throws InterruptedException {
        mDumpcatcher = new Dumpcatcher(PRODUCT_KEY, SECRET, "http://localhost:1/add", CLIENT, 2);
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                throw new MyRuntimeException("BOOM!");
            }
        });

        thread.setUncaughtExceptionHandler(new DumpcatcherUncaughtExceptionHandler(mDumpcatcher));
        thread.start();
        thread.join();
        // TODO(jlapenna): Actually somehow check that the message ended up getting logged.
    }

    private static class MyRuntimeException extends RuntimeException {
        private static final long serialVersionUID = 1L;

        public MyRuntimeException(String msg) {
            super(msg);
        }
    }
}
